﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TimeSolve
{
    /// <summary>
    /// CoordinateSystem.xaml 的交互逻辑
    /// </summary>
    public partial class CoordinateSystem : UserControl
    {
        public CoordinateSystem()
        {
            InitializeComponent();
            CreatGeometry cg = new CreatGeometry();
            
            //Viewport3D vCoordinate = new Viewport3D ();
            this.camera = new OrthographicCamera()
            {
                Position = new Point3D(0, 0, 60),
                LookDirection = new Vector3D(0, 0, -1),
                Width = 70,
                UpDirection = new Vector3D(0, 1, 0)
            };
            vcoordinate.Camera = this.camera;
            vcoordinate.Children.Add(cg.creatCoordinateModel());
        }
        
        OrthographicCamera camera { get; set; }
        private Point tagCenter;
        private double rate;

        public double CoordinateSize
        {
            get { return (double)GetValue(CoordinateSizeProperty); }
            set { SetValue(CoordinateSizeProperty, value); }
        }
        public static readonly DependencyProperty CoordinateSizeProperty =
            DependencyProperty.Register("CoordinateSize", typeof(double), typeof(CoordinateSystem), new PropertyMetadata(0.0));


        public void ViewFollow(Camera sourceCamera_norm)
        {
            Vector3D lookdir;
            Vector3D updir;
            OrthographicCamera oc = sourceCamera_norm as OrthographicCamera;
            if (oc != null)
            {
                lookdir = oc.LookDirection;
                updir = oc.UpDirection;
            }else
            {
                PerspectiveCamera pc = sourceCamera_norm as PerspectiveCamera;
                if (pc != null)
                {
                    lookdir = pc.LookDirection;
                    updir = pc.UpDirection;
                }
                else
                {
                    return;
                }
            }
            camera .Position = (Point3D)(-60 * lookdir);
            camera.UpDirection = updir;
            camera.LookDirection = lookdir;
            Vector3D right1 = Vector3D.CrossProduct(camera.LookDirection, camera.UpDirection);
            Action<Vector3D, UIElement> action = (v, tag) =>
            {
                double vup = Vector3D.DotProduct(v, camera.UpDirection);
                double vright = Vector3D.DotProduct(v, right1);
                Canvas.SetTop(tag, tagCenter.Y - vup * rate);//canvas坐标左上为原点
                Canvas.SetLeft(tag, tagCenter.X + vright * rate);
            };
            action.Invoke(new Vector3D(30, 0, 0), vAxisX);
            action.Invoke(new Vector3D(0, 30, 0), vAxisY);
            action.Invoke(new Vector3D(0, 0, 30), vAxisZ);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            double half = this.CoordinateSize / 2;
            this.tagCenter = new Point(half - vAxisX.Width / 2, half - vAxisX.Height / 2);
            this.rate = this.Width / camera.Width;

            this.camera.LookDirection.Normalize();
            this.camera.UpDirection.Normalize();
            ViewFollow(this.camera);
        }
    }
}
